﻿using System;
using System.Collections.Generic;
using System.Web;
using Microsoft.Web.Administration;
using System.Diagnostics;
using System.DirectoryServices;

namespace LCL.IISManagers
{
    public class IISWorker
    {
        private string strServer = "localhost";

        /// <summary>
        /// 获取所有站点
        /// </summary>
        /// <returns></returns>
        public List<WebSiteModel> GetAllSite()
        {
            List<WebSiteModel> list = new List<WebSiteModel>();
            ServerManager manager = new ServerManager();
            SiteCollection sites = manager.Sites;

            foreach (Site item in sites)
            {
                WebSiteModel model = new WebSiteModel();
                try
                {
                    model.Title = item.Name;
                    model.Path = item.Applications["/"].VirtualDirectories["/"].PhysicalPath;
                    if (item.Bindings[0].Protocol == "ftp")
                    {
                        string port = item.Bindings[0].BindingInformation.Replace("*", "").Replace(":", "");
                        model.Type = "ftp";
                        model.Url = "localhost:" + port;
                        model.Port = int.Parse(port);
                    }
                    else
                    {
                        model.Type = "http";
                        model.Url = "localhost:" + item.Bindings[0].EndPoint.Port;
                        model.Port = item.Bindings[0].EndPoint.Port;
                    }
                    model.AppPool = GetAppPoolAll(item.Applications["/"].ApplicationPoolName);
                    model.IISVersion = "7";
                    model.State = GetState(item);
                    foreach (Application app in item.Applications)
                    {
                        if (app.Path != "/")
                        {
                            WebSiteModel child = new WebSiteModel();
                            child.Title = app.Path;
                            child.Path = app.VirtualDirectories["/"].PhysicalPath;
                            child.Type = app.EnabledProtocols;
                            child.Port = item.Bindings[0].EndPoint.Port;
                            if (item.Bindings[0].EndPoint.Port == 80)
                            {
                                child.Url = app.Path;
                            }
                            else
                            {
                                child.Url = "localhost:" + item.Bindings[0].EndPoint.Port + app.Path;
                            }
                            child.IISVersion = "7";
                            child.State = model.State;
                            child.AppPool = GetAppPoolAll(app.ApplicationPoolName);
                            model.Children.Add(child);
                        }
                    }
                    list.Add(model);
                }
                catch (Exception ex)
                {
                    Debug.Write(model.Title + " error:" + ex.Message);
                }
            }
            return list;
        }
        private string GetState(Site item)
        {
            string strstate = "正常";
            try
            {
                switch (item.State)
                {
                    case ObjectState.Started:
                        strstate = "正常";
                        break;
                    case ObjectState.Starting:
                        strstate = "正在启动";
                        break;
                    case ObjectState.Stopped:
                        strstate = "关闭";
                        break;
                    case ObjectState.Stopping:
                        strstate = "正在关闭";
                        break;
                    case ObjectState.Unknown:
                        strstate = "未知的";
                        break;
                    default:
                        strstate = "正常";
                        break;
                }
            }
            catch (Exception)
            {
                strstate = "正常";
            }
            return strstate;
        }
        private string GetAppPoolAll(string appPoolName)
        {
            string temp = "";
            var appool = GetAppPool(appPoolName);
            if (appool != null)
            {
                temp = appool.Name + " " + appool.ManagedRuntimeVersion;
            }
            return temp;
        }
        private ApplicationPool GetAppPool(string appPoolName)
        {
            ApplicationPool app = null;
            var appPools = new DirectoryEntry(string.Format("IIS://{0}/W3SVC/AppPools", "localhost"));
            foreach (DirectoryEntry entry in appPools.Children)
            {
                if (entry.Name == appPoolName)
                {
                    var manager = new ServerManager();
                    app = manager.ApplicationPools[appPoolName];
                    break;
                }
            }
            return app;
        }
    }

    public class WebSiteModel
    {
        public WebSiteModel()
        {
            Children = new List<WebSiteModel>();
        }
        public string Type { get; set; }
        public string Title { get; set; }
        public int Port { get; set; }
        public string Url { get; set; }
        public string Path { get; set; }
        public string IISVersion { get; set; }
        public string State { get; set; }
        /// <summary>
        /// 应用程序池
        /// </summary>
        public string AppPool { get; set; }
        /// <summary>
        /// 子站点
        /// </summary>
        public List<WebSiteModel> Children { get; set; }
    }
}